package Controller.cart;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Controller.BaseHttpServlet;
import Entity.Book;
import Entity.BookInvoice;

import Services.WebService;
public class HomeServlet extends BaseHttpServlet {
	private ArrayList<BookInvoice> bookOrderList;
	
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) throws ServletException, IOException {
    	this.setupOrder(request, webServices);
    }

	private void setupOrder(HttpServletRequest request, WebService webServices) {
		HttpSession session = request.getSession();
		
		int bookID, quantity, i=1;
		do{
			Object temp = session.getAttribute("cart.book"+i);
			bookID = Integer.valueOf(temp.toString());
			
			temp = session.getAttribute("cart.book"+i+".quantity");
			quantity = Integer.valueOf(temp.toString());
			
			Book book = webServices.getEntityManager().getBook(bookID);
			
			this.bookOrderList.add(new BookInvoice(book, quantity));
		}while(bookID>0);
	}
}